<script setup lang="ts">
import type { ConsultType } from '@/enum'
import { getRecordList } from '@/services/consult'
import type { ConsultRecord, recordList } from '@/types/consult'
import { ref } from 'vue'
import RecordItem from './RecordItem.vue'
const props = defineProps<{
  type: ConsultType
}>()

// 请求参数
const paramsConfig = ref<ConsultRecord>({
  current: 1,
  pageSize: 10,
  type: props.type
})
const recordLists = ref<recordList[]>([])
let total = ref(0)
const loading = ref(false)
const finished = ref(false)
const onLoad = async () => {
  // 触底加载数据
  // 需要判断下拉刷新的状态，如果是已经下拉刷新过后的状态，则需要让current+1，否则就是初始值
  if (refreshFlag.value) {
    paramsConfig.value.current = 2
    refreshFlag.value = false
  }
  const { data } = await getRecordList(paramsConfig.value)
  total.value = data.total
  recordLists.value.push(...data.rows)
  loading.value = false
  // 判断是否加载完成
  if (paramsConfig.value.current * paramsConfig.value.pageSize > total.value) {
    finished.value = true
  } else {
    paramsConfig.value.current++
  }
}
// 下拉刷新
const refreshLoading = ref(false)
const refreshFlag = ref(false)
const onRefresh = async () => {
  // 获取最新的数据，重新覆盖之前的数据
  paramsConfig.value.current = 1
  const { data } = await getRecordList(paramsConfig.value)
  recordLists.value = data.rows
  refreshLoading.value = false
  refreshFlag.value = true
}
// 删除订单
const delOrder = (id: string) => {
  // 从recordLists中删除订单id符合的这一项
  recordLists.value = recordLists.value.filter((item) => item.id !== id)
}
</script>

<template>
  <van-pull-refresh v-model="refreshLoading" @refresh="onRefresh">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <record-item
        v-for="item in recordLists"
        :key="item.id"
        :item="item"
        @del-order="delOrder"
      ></record-item>
    </van-list>
  </van-pull-refresh>
</template>

<style lang="scss" scoped>
.van-list {
  padding: 10px 15px;
}
</style>
